(function($) {
	var o = $({}); //自定义事件对象
	$.each({
		trigger: 'publish',
		on: 'subscribe',
		off: 'unsubscribe'
	}, function(key, val) {
		jQuery[val] = function() {
			o[key].apply(o, arguments);
		};
	});
})(jQuery);

var tag = this,
	observer = riot.observable({});
$.parser.onComplete = function() {
	$("#loading").fadeOut("slow", function() {
		$(this).remove();
	});
}
$(function() {
	
	riot.mount('*', {
		observer: observer
	});

	$.subscribe('app:logout', function(e, isForce = false) {
		if (isForce) {
			window.location.href = '/user/logout';
			return;
		}
		$.messager.confirm('注意', '<b>是否登出系统?</b><br/>登出系统后，未保存的数据将会丢失，请确认所有改动都已保存！', function(r) {
			if (r) {
				window.location.href = '/user/logout';
			}
		});
	});
	$.subscribe('app:update', function(e) {
		$.publish('toastr:show', {
			type: 'success',
			title: '提醒',
			message: '系统升级成功！'
		});
	});
	$.subscribe('process:start', function(e, arg = {
		title: '提示',
		text: '努力加载中...'
	}) {
		$.messager.progress({
			title: arg.title,
			text: arg.text
		});
		setTimeout(function() {
			$.publish('process:end');
		}, 10000);
	});
	$.subscribe('process:end', function(e) {
		$.messager.progress('close');
	});
	$.subscribe('toastr:show', function(e, arg = {
		type: 'success',
		title: '',
		message: ''
	}) {
		toastr[arg.type](arg.message, arg.title, {
			progressBar: true,
			closeButton: true
		});
	});
});


//获取cookie
function getCookie(name) {
	var cookies = document.cookie.split(';');
	if (!cookies.length) return '';
	for (var i = 0; i < cookies.length; i++) {
		var pair = cookies[i].split('=');
		if ($.trim(pair[0]) == name) {
			return $.trim(pair[1]);
		}
	}
	return '';
}